home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libk.lha / Lib / KRB / GETKRBHS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-24  |  2.5 KB  |  87 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/krb/RCS/get_krbhst.c,v $
  3.  * $Author: rfrench $
  4.  *
  5.  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
  6.  * of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  */
  11.  
  12. #ifndef lint
  13. static char *rcsid_get_krbhst_c =
  14. "$Header: get_krbhst.c,v 4.8 89/01/22 20:00:29 rfrench Exp $";
  15. #endif /* lint */
  16.  
  17. #include <mit_copy.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <krb.h>
  21.  
  22. /*
  23.  * Given a Kerberos realm, find a host on which the Kerberos authenti-
  24.  * cation server can be found.
  25.  *
  26.  * krb_get_krbhst takes a pointer to be filled in, a pointer to the name
  27.  * of the realm for which a server is desired, and an integer, n, and
  28.  * returns (in h) the nth entry from the configuration file (KRB_CONF,
  29.  * defined in "krb.h") associated with the specified realm.
  30.  *
  31.  * On end-of-file, krb_get_krbhst returns KFAILURE.  If n=1 and the
  32.  * configuration file does not exist, krb_get_krbhst will return KRB_HOST
  33.  * (also defined in "krb.h").  If all goes well, the routine returnes
  34.  * KSUCCESS.
  35.  *
  36.  * The KRB_CONF file contains the name of the local realm in the first
  37.  * line (not used by this routine), followed by lines indicating realm/host
  38.  * entries.  The words "admin server" following the hostname indicate that 
  39.  * the host provides an administrative database server.
  40.  *
  41.  * For example:
  42.  *
  43.  *    ATHENA.MIT.EDU
  44.  *    ATHENA.MIT.EDU kerberos-1.mit.edu admin server
  45.  *    ATHENA.MIT.EDU kerberos-2.mit.edu
  46.  *    LCS.MIT.EDU kerberos.lcs.mit.edu admin server
  47.  *
  48.  * This is a temporary hack to allow us to find the nearest system running
  49.  * kerberos.  In the long run, this functionality will be provided by a
  50.  * nameserver.
  51.  */
  52.  
  53. krb_get_krbhst(h,r,n)
  54.     char *h;
  55.     char *r;
  56.     int n;
  57. {
  58.     FILE *cnffile;
  59.     char tr[REALM_SZ];
  60.     char linebuf[BUFSIZ];
  61.     register int i;
  62.  
  63.     if ((cnffile = fopen(KRB_CONF,"r")) == NULL) {
  64.         if (n==1) {
  65.             (void) strcpy(h,KRB_HOST);
  66.             return(KSUCCESS);
  67.         }
  68.         else
  69.             return(KFAILURE);
  70.     }
  71.     if (fscanf(cnffile,"%s",tr) == EOF)
  72.         return(KFAILURE);
  73.     /* run through the file, looking for the nth server for this realm */
  74.     for (i = 1; i <= n;) {
  75.     if (fgets(linebuf, BUFSIZ, cnffile) == NULL) {
  76.             (void) fclose(cnffile);
  77.             return(KFAILURE);
  78.         }
  79.     if (sscanf(linebuf, "%s %s", tr, h) != 2)
  80.         continue;
  81.         if (!strcmp(tr,r))
  82.             i++;
  83.     }
  84.     (void) fclose(cnffile);
  85.     return(KSUCCESS);
  86. }
  87.